<?php

namespace Rexzy\Http;

class Response {
    private $_httpVersion;
    private $_charset;
    private $_header = array();
    private $_statusMessage = array(
        // 请求消息

        100 => 'Continue',

        101 => 'Switching Protocols',
        //成功

        200 => 'OK',

        201 => 'Created',
        202 => 'Accepted',
        203 => 'Non-Authoritative Information',
        204 => 'No Content',
        205 => 'Reset Content',
        206 => 'Partial Content',
        // 重定向

        300 => 'Multiple Choices',

        301 => 'Moved Permanently',
        302 => 'Found',
        303 => 'See Other',
        304 => 'Not Modified',
        305 => 'Use Proxy',
        306 => '(Unused)',
        307 => 'Temporary Redirect',
        // 请求错误

        400 => 'Bad Request',

        401 => 'Unauthorized',
        402 => 'Payment Required',
        403 => 'Forbidden',
        404 => 'Not Found',
        405 => 'Method Not Allowed',
        406 => 'Not Acceptable',
        407 => 'Proxy Authentication Required',
        408 => 'Request Timeout',
        409 => 'Conflict',
        410 => 'Gone',
        411 => 'Length Required',
        412 => 'Precondition Failed',
        413 => 'Request Entity Too Large',
        414 => 'Request-URI Too Long',
        415 => 'Unsupported Media Type',
        416 => 'Requested Range Not Satisfiable',
        417 => 'Expectation Failed',
        //服务器错误

        500 => 'Internal Server Error',

        501 => 'Not Implemented',
        502 => 'Bad Gateway',
        503 => 'Service Unavailable',
        504 => 'Gateway Timeout',
        505 => 'HTTP Version Not Supported'
    );
    
    public function __construct( $httpVersion = '1.1', $charset = 'utf-8' ) {
        $this->_httpVersion = $httpVersion;
        $this->_charset = $charset;
    }
    
    /**
     * 设置 Response 输出头和打印内容（当 headers_sent 为true时，只打印内容，不设置）
     * 
     * @param string $body 打印内容
     * @param string $status 设置输出状态值
     * @param string $contentType 设置输出内容格式
     */
    public function send( $body = '', $status = null, $contentType = null ) {
        if ( ! headers_sent() ) {
            header( "HTTP/$this->_httpVersion $status {$this->_statusMessage[$status]}");
            header( "Content-Type:$contentType;charset=$this->_charset");
            if ( count( $this->_header ) > 0 ) {
                foreach ( $this->_header as $key => $value ) {
                    header( "$key:$value" );
                }
            }
        }
        echo $body;
    }
    
    /**
     * 常规 Response 输出头内容的设置函数
     * 
     * @param string $key 设置项的键名
     * @param string $value 设置项的值
     * 
     * @return object 返回当前对象，实现链式操作
     */
    public function header( $key, $value ) {
        $this->_header[ $key ] = $value;
        return $this;
    }
    
    /**
     * header（Location）跳转函数
     * 
     * @param string $url 跳转链接
     */
    public function location( $url ) {
        $this->header( 'Location', $url )->send( '', 301 );
        exit;
    }
}
